package org.eduapp.trains.dao.account.impl;

import java.util.List;

import org.eduapp.trains.DAOException;
import org.eduapp.trains.dao.BaseDAOImpl;
import org.eduapp.trains.dao.account.AccountDAO;
import org.eduapp.trains.model.account.ModelAccount;
import org.eduapp.trains.utils.UtilsString;
import org.eduapp.trains.web.support.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 * 
 * @author jeccy
 *
 */
public class AccountDAOImpl extends BaseDAOImpl<ModelAccount> implements AccountDAO {
	
	@SuppressWarnings("unchecked")
	@Override
	public ModelAccount getAccountByNo(String accountNo) throws DAOException {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAccount.class);
		
		if (UtilsString.isNotEmpty(accountNo)) {
			criteria.add(Restrictions.ilike("accountNo", accountNo, MatchMode.ANYWHERE));
		}
		
		List<ModelAccount> result = getListByCriteria(criteria);
		
		if (result != null && result.size() > 0) {
			if (result.size() == 1) {
				return result.get(0);
			} else {
				//TODO error should be raised
				return null;
			}
		}
		
		return null;
	}
	
	@Override
	public PaginationSupport<ModelAccount> get(ModelAccount model,
			int pageSize, int startIndex, String order, Boolean isDesc)
			throws DAOException {
		
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelAccount.class);
		
		if (model != null) {
			
			if (model.getAccountStatus() != null) {
				criteria.add(Restrictions.eq("accountStatus", model.getAccountStatus()));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountClass())) {
				criteria.add(Restrictions.ilike("accountClass", model.getAccountClass(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountNo())) {
				criteria.add(Restrictions.ilike("accountNo", model.getAccountNo(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountDiscountFrom())) {
				criteria.add(Restrictions.ilike("accountDiscountFrom", model.getAccountDiscountFrom(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountDiscountTo())) {
				criteria.add(Restrictions.ilike("accountDiscountTo", model.getAccountDiscountTo(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountIdentification())) {
				criteria.add(Restrictions.ilike("accountIdentification", model.getAccountIdentification(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountMobilePhone())) {
				criteria.add(Restrictions.ilike("accountMobilePhone", model.getAccountMobilePhone().toString(), MatchMode.ANYWHERE));
			}
			
			if (model.getAccountSchool() != null 
					&& UtilsString.isNotEmpty(model.getAccountSchool().getId())) {
				criteria.createCriteria("accountSchool").add(Restrictions.eq("id", model.getAccountSchool().getId()));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountGrade())) {
				criteria.add(Restrictions.ilike("accountGrade", model.getAccountGrade(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountMajority())) {
				criteria.add(Restrictions.ilike("accountMajority", model.getAccountMajority(), MatchMode.ANYWHERE));
			}
			
			if (UtilsString.isNotEmpty(model.getAccountRealName())) {
				criteria.add(Restrictions.ilike("accountRealName", model.getAccountRealName(), MatchMode.ANYWHERE));
			}
			
		}
		
		if (UtilsString.isNotEmpty(order)) {
			if (isDesc != null && isDesc) {
				criteria.addOrder(Order.desc(order));
			} else {
				criteria.addOrder(Order.asc(order));
			}
		} else {
			criteria.addOrder(Order.desc("accountNo"));
		}
		
		return findPageByCriteria(criteria, pageSize, startIndex);
	}

	@Override
	public List<ModelAccount> getByModel(ModelAccount model)
			throws DAOException {
		
		PaginationSupport<ModelAccount> result = get(model, -1, 0, null, null);
		
		if (result != null && result.getItemCount() > 0) {
			return result.getItems();
		}
		
		return null;
	}

}
